package com.anttech.shoedemo;

import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.anttech.sdk.enummode.ShoeDevice;
import com.anttech.sdk.listener.BleTaskListener;
import com.anttech.sdk.listener.DeviceStateListener;
import com.anttech.sdk.pub.AntsBleSdkManager;
import com.larswerkman.holocolorpicker.ColorPicker;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends Activity {
    private final String TAG = "MainActivity";

    @Bind(R.id.connect_tv)
    TextView connectTv;
    @Bind(R.id.activity_main)
    LinearLayout activityMain;
    AntsBleSdkManager sdkManager = AntsBleSdkManager.getInstance();
    Context mContext;

    BleTaskListener listener = new BleTaskListener() {
        @Override
        public void onSuccess(String jsonData) {
            Log.v(TAG, jsonData);
        }

        @Override
        public void onFailed(String jsonData) {
            Log.v(TAG, jsonData);
        }
    };
    @Bind(R.id.mode_listview)
    ListView modeListview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        ButterKnife.bind(this);
        initPickView();
        BluetoothDevice device = (BluetoothDevice) getIntent().getParcelableExtra("BluetoothDevice");
        sdkManager.registerDeviceStateLisener(new DeviceStateListener() {
            @Override
            public void connected(String deviceName) {
                connectTv.setText(deviceName + "已连接");
            }

            @Override
            public void disConnected(String deviceName) {
                connectTv.setText(deviceName + "连接已断开");
            }
        });
        connectTv.setText("正在连接");
        sdkManager.bindDevice(device.getAddress(), device.getName());
        modeListview.setAdapter(new SelectModeListAdapter(mContext));
        modeListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                sdkManager.changeShoeLightMode(listener, ShoeDevice.R_SHOE,position+1);
            }
        });


    }

    private void initPickView() {
        ColorPicker picker = (ColorPicker) findViewById(R.id.picker);
        picker.getColor();
        picker.setOldCenterColor(picker.getColor());
        picker.setShowOldCenterColor(false);
        picker.setOnColorSelectedListener(new ColorPicker.OnColorSelectedListener() {
            @Override
            public void onColorSelected(int color) {
                sdkManager.changeShoeLightColor(listener, ShoeDevice.R_SHOE, color);
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


}
